//
//  CollectionViewCell.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/13.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "CollectionViewCell.h"
#import "ZFMDBTool.h"
@implementation CollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self foundDataBank];
        [self initlizeInterface];
        
    }
    return self;
}

- (void)initlizeInterface {
    self.backgroundColor = KHColorFromRGB(0xf8f8f8);
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.contextLabel];
    [self.contentView addSubview:self.redView];
//    self.redView.hidden = YES;
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.width.offset(52*xsIZE);
        make.height.offset(60*ysIZE);
        make.top.equalTo(self.contentView).offset(10*ysIZE);
    }];
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView);
        make.right.equalTo(self).offset(-10*xsIZE);
        make.width.height.offset(6*ysIZE);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.imageView);
        make.top.equalTo(self.imageView.mas_bottom);
        make.width.offset(70*ysIZE);
        make.height.offset(15*ysIZE);
    }];
    [self.contextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(5*xsIZE);
        make.right.equalTo(self.contentView).offset(-5*xsIZE);
        make.height.offset(40*ysIZE);
        make.top.equalTo(self.titleLabel.mas_bottom);
    }];
    
}

- (void)setPImage:(UIImage *)pImage {
    _pImage = pImage;
    self.imageView.image = _pImage;

}
- (void)setVname:(NSString *)Vname {
    _Vname = Vname;
//    [self find];
}
- (void)setUrl:(NSString *)url {
    _url = url;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:_url] placeholderImage:[UIImage imageNamed:_pimageName] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        [self findImageWith:image];
    }];
}


// 打开数据库
-(void)foundDataBank; {
    [[ZFMDBTool sharedInstance] execSqlInFmdb:@"DoctorFAQs" dbFileName:@"Home.sqlite" dbHandler:^(FMDatabase *Z_db) {
        NSString *cSql = @"CREATE TABLE IF NOT EXISTS Home (id INTEGER PRIMARY KEY, imageName TEXT NOT NULL,image blob)";
        BOOL res = [Z_db executeUpdate:cSql];
        if (!res) {
            NSLog(@"error when creating db table");
        } else {
            NSLog(@"succ to creating db table");
        }
    }];
}

// 查询 并赋值
- (void)find {
    [[ZFMDBTool sharedInstance] execSqlInFmdb:@"DoctorFAQs" dbFileName:@"Home.sqlite" dbHandler:^(FMDatabase *Z_db) {
        NSString *qSql = [NSString stringWithFormat:@"SELECT * FROM Home where imageName like '%@'",self.Vname];
        FMResultSet *set = [Z_db executeQuery:qSql];
        while ([set next]) {
            NSData * imageData = [set dataForColumn:@"image"];
            UIImage * image = [UIImage imageWithData:imageData];
            self.imageView.image = image;
        }
        
    }];
    
}

- (void)findImageWith:(UIImage *)image{
    [[ZFMDBTool sharedInstance] execSqlInFmdb:@"DoctorFAQs" dbFileName:@"Home.sqlite" dbHandler:^(FMDatabase *Z_db) {
        NSString *qSql = [NSString stringWithFormat:@"SELECT * FROM Home where imageName like '%@'",self.Vname];
        FMResultSet *set = [Z_db executeQuery:qSql];
        while ([set next]) {
            NSData * imageData = [set dataForColumn:@"image"];
            if (!imageData) {
                //如果不存在imagedata 则插入
                [self inserctWith:image];
            }else {
                [self upDataWith:image];
            }
        }
        
    }];
    
}
// 插入
- (void)inserctWith:(UIImage*)image {
    [[ZFMDBTool sharedInstance] execSqlInFmdb:@"DoctorFAQs" dbFileName:@"Home.sqlite" dbHandler:^(FMDatabase *Z_db) {
        NSString * sql = @"insert into Home (imageName,image) values(?,?)";
        NSData * imageData = UIImageJPEGRepresentation(image, 1);
//        NSLog(@"seaasd%@",self.Vname);
        BOOL res = [Z_db executeUpdate:sql,self.Vname,imageData];
        if (!res) {
            NSLog(@"error to insert data");
        } else {
            NSLog(@"succ to insert data");
        }
    }];
}
// up更新
- (void)upDataWith:(UIImage *)image {
    [[ZFMDBTool sharedInstance] execSqlInFmdb:@"DoctorFAQs" dbFileName:@"Home.sqlite" dbHandler:^(FMDatabase *Z_db) {
         NSString * sql = @"UPDATE Home SET image = ? WHERE  imageName = ?";
        BOOL res = [Z_db executeUpdate:sql,image,self.Vname];
        if (!res) {
            NSLog(@"error to UPDATE data");
        } else {
            NSLog(@"succ to UPDATE data");
        }
    }];
}


// lazy
- (UIImageView *)imageView {
    if (_imageView) {
        return _imageView;
    }
    _imageView = [[UIImageView alloc] init];
//    _imageView.image = [UIImage imageNamed:@""];
    return _imageView;
}
- (UILabel *)titleLabel {
    if (_titleLabel ) {
        return _titleLabel;
    }
    _titleLabel = [[UILabel alloc] init];
//    _titleLabel.text = @"咨询医生";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    return _titleLabel;
}
- (UIView *)redView {
    if (_redView) {
        return _redView;
    }
    _redView = [[UIView alloc] init];
    _redView.backgroundColor = [UIColor redColor];
    _redView.hidden = YES;
    _redView.layer.cornerRadius = 3;
    _redView.layer.masksToBounds = YES;
    return _redView;
}
- (UILabel *)contextLabel {
    if (_contextLabel) {
        return _contextLabel;
    }
    _contextLabel = [[UILabel alloc] init];
    
    _contextLabel.numberOfLines = 2;
    _contextLabel.textColor = KHGaryColor;
    _contextLabel.font = [UIFont systemFontOfSize:12];
    return _contextLabel;
}
@end
